﻿$(function() {
	var weekpie = echarts.init(document.getElementById('weekpie'));
	optionpie = {
		    tooltip: {
		        trigger: 'item',
		        formatter: "{a} <br/>{b}: {c} ({d}%)"
		    },
		    backgroundColor:'#e0e0e0',
		    legend: {
		        orient: 'vertical',
		        x: 'right',
		        data:[]
		    },
		    series: [{
	            name:'电费',
	            type:'pie',
	            radius: [0, '40%'],
	            label: {
	                normal: {
	                    position: 'inside'
	                }
	            },
	            labelLine: {
	                normal: {
	                    show: false
	                }
	            },
	            data:[]
	        },{
	            name:'电度',
	            type:'pie',
	            radius: ['55%', '80%'],
	            data:[]
	        }]
		};
	weekpie.setOption(optionpie);

	
    var weekbar = echarts.init(document.getElementById('weekbar'));
    optionbar = {
    	    tooltip : {
    	        trigger: 'axis',
    	        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
    	            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
    	        }
    	    },
    	    legend: {
    	    	bottom: '20',
    	        data:[],
    	    },
    	    grid: {
	            top: '10%',
	            left: '4%',
	            right: '4%',
	            bottom:'20%',
	            containLabel: true
	        },
    	    backgroundColor:'#e0e0e0',
    	    xAxis : [{
    	            type : 'category',
    	            data : [],
    	            splitLine:{
    	            	show:false
    	            },
//    	            axisLabel:{
//   		    		 formatter: '{value}周'
//   		    	 },
	        }],
	        yAxis: [{
		    	 type: 'value',
		    	 name:'电度(度)',
		    	 axisLabel:{
		    		 formatter: '{value}'
		    	 },
		    	 splitLine:{
		            	show:false
		            }
		    },{
    	    	type: 'value',
		    	name:'电费(元)',
		    	axisLabel:{
		    		formatter: '{value}'
		    	},
		    	splitLine:{
	            	show:false
		        }
    	    }],
    	    series : []
    	};
    weekbar.setOption(optionbar);
    
    var lineId=0;
    var	startWeek="";
    var	endWeek="";
    var year=moment().year();
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
	       	startWeek=value[0];
	       	endWeek=value[1];
	       	getData();
       }
    });
    
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	}); 
    var currentWeek=moment().week();
    var weeks="";
    if(currentWeek<8){
    	weeks=[1,currentWeek];
    }else{
    	weeks=[moment().subtract(7, 'w').week(),currentWeek];
    }
    $('#ss').slider('setValues',weeks);
    var arr=$('#ss').slider('getValues');
    startWeek=arr[0];
    endWeek=arr[1];
    getData();
    function getData(){
    	$.post(basePath+"/line/lineElectricity/week.json",{"lineId":lineId,year:year,startTime:startWeek,endTime:endWeek},function(res){
    		vm.$set('item',res);
    		if(res.success){
    			weekpie.setOption({
    				legend:{
    					data:res.legType
    				},
			        series: [{
			        	name:'电费',
			            data: res.chargePie
			        },{
			        	name:"电度",
			            data: res.electricityPie
			        }]
    			});
    			var series1=[];
    			series1.push({
    				name:res.chargeBar[0].name,
    	            type:'bar',
    	            stack: '电度',
    	            barMaxWidth:40,
    	            data:res.chargeBar[0].value//[50, 80, 60, 55, 60, 48, 72,50, 80, 60, 55]
		        });
    		    for(var i = 1;i<res.chargeBar.length;i++){
    		        series1.push({
    		            name: res.chargeBar[i].name,
    		            stack: '电费',
    		            yAxisIndex: 1,
    		            type: 'bar',
    		            barMaxWidth:40,
    		            data: res.chargeBar[i].value
    		        });
    		    }
    			weekbar.setOption({
    				legend:{
    					data:res.barLegend
    				},
    				xAxis : [{
        	            data : res.weeks//['10周','11周','12周','13周','14周','15周','16周','17周','18周','19周','20周']
        	        }],
        	        series:series1
    			});
    		}else{
    			alert("无数据，请重新选择时间区间");
    		}
    	});
    }
    
  //点击事件
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	//定义时间的表单
	$('#defineTimeWin').dialog({
		title:'自定义时间',
		width:400,
		height:150,
		iconCls:'icon-add',
		closed: true,
		modal: true,
		inline:true,
		buttons:[{
			text:'确定',
			iconCls:'icon-add-new',
			handler:function(){
					var st=$("input[name='startTime']").val();
					var cmax=moment(st, "YYYY").weeksInYear();
					year=st;
					$('#ss').slider({
						max:cmax
					});
					getData();
					$('#defineTimeWin').dialog('close');

			}
		},{
			text:'取消',
			iconCls:'icon-redo',
			handler:function(){
				$('#defineTimeWin').dialog('close');
			}
		}],
		onClose: function() {
			$('#defineTimeform').form('reset');
		}
	});
	 /**
	  * 切换设备的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onDblClick: function(node) {
			if (node.url) {
				if(node.shifts){
					lineId=node.id;
		 			var arr=$('#ss').slider('getValues');
		 			startWeek=arr[0];
		 			endWeek=arr[1];
		 			getData();
		 			mePanel.dialog('close');

				}else{
					$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
				}
				lineId=node.id;
			}else{
				$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
			}
		}
	});
 $("#switchEquipOK").click(function(){
 	var row = $("#linetree").tree('getSelected');
 	if(row.url){
		lineId=row.id;
		var arr=$('#ss').slider('getValues');
		startWeek=arr[0];
		endWeek=arr[1];
		getData();
		mePanel.dialog('close');
 	}else{
		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
	}
 });
});